package com.example.campuslink.commen.adpter

import android.util.Log
import android.widget.ImageView
import android.widget.RadioButton
import com.bumptech.glide.Glide
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.core.CoreAdapter
import com.example.campuslink.commen.core.CoreViewHolder
import com.example.campuslink.databinding.AdapterPhotoBinding
import com.example.campuslink.model.ChoiceFilePathData
import com.example.campuslink.viewModel.PhotoViewModel

class PhotoAdapter(vm: PhotoViewModel) :
    CoreAdapter<AdapterPhotoBinding, PhotoViewModel, ChoiceFilePathData>(
        BR.data,
        BR.viewModel,
        vm
    ) {

    private val allFilePath = mViewModel?.allFilePath?.value
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_photo
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)

        Glide.with(holder.itemView.context).load(mListData[position].path)
            .error(R.drawable.ic_empty)
            .into(holder.binding.root.findViewById<ImageView>(R.id.iv_photo))
        if (mListData[position].isChoice) {
            holder.binding.root.findViewById<RadioButton>(R.id.rb_choice).isChecked = true
        } else {
            holder.binding.root.findViewById<RadioButton>(R.id.rb_choice).isChecked = false
        }
        holder.binding.root.findViewById<RadioButton>(R.id.rb_choice).setOnClickListener({

            if (mListData[position] in allFilePath!!) {
                allFilePath.remove(mListData[position])
                mListData[position].isChoice = false
            } else {
                allFilePath.add(mListData[position])
                mListData[position].isChoice = true
            }
            mViewModel?.changeFilePath(allFilePath)

            notifyItemChanged(position)
        })
        holder.binding.root.setOnClickListener {
//            val allFilePath = mViewModel?.allFilePath?.value
            if (mListData[position] in allFilePath!!) {
                allFilePath.remove(mListData[position])
                mListData[position].isChoice = false
            } else {
                allFilePath.add(mListData[position])
                mListData[position].isChoice = true
            }
            mViewModel?.changeFilePath(allFilePath)

            notifyItemChanged(position)
        }
    }
}